Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: '',
    listdata: [],
    zc1: '',
    zc2: '',
    active:'a'
  },

  onChange1(event) {
    this.setData({
      username: event.detail.replace(/\s+/g, '')
    })
  },
  // xxb
  onChange2(event) {
    // event.detail 为当前输入的值
    this.setData({
      password: event.detail.replace(/\s+/g, '')
    })
  },

  back() {
    wx.redirectTo({
      url: '/pages/yindao/yindao'
    })
  },
  // xxb 登录校验
  getLogins(){
    wx.request({
      url: 'http://localhost:7001/api/getLogin',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: (res)=> {
        this.setData({
          listdata: res.data.data
        })

      },
      fail: function () {
        wx.showToast({
          icon: "none",
          mask: true,
          title: "接口调用失败，请稍后再试。",
        });
      }
    })
  },
  onLoad(options) {
    
    let token=wx.getStorageSync('token')
    if(token){
       wx.switchTab({
         url: '../home/home',
       })
    }
    this.getLogins()
  },
  onReady() {},
  // 校验是否为全数字
  xiaoyan(val){
    if(isNaN(val)){
       return false 
    }else{
      return true
    }
  },
  // 登录
  login() {
    let that = this
    let {username,password,listdata}=this.data
   listdata.filter((item) => {
      if(username == ''&&password==''){
        wx.showToast({
              icon: "none",
              mask: true,
              title: "登录失败，用户名或密码不能为空",
           })
      }else{
        if (item.username == this.data.username && item.password == this.data.password) {
          wx.request({
            url: 'http://localhost:7001/api/addMyUser',
            method: 'post',
            data: {
              username: this.data.username,
              password: this.data.password
            },
            success: function (res) {
              wx.setStorageSync('usename', that.data.username)
              wx.setStorageSync('token', res.data.data.token)
              wx.setStorageSync('userid', item.userid)
              wx.showToast({
                icon: "none",
                mask: true,
                title: "登录成功",
              });
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/home/home',
                })
              }, 2000)
            }
          })
        }else{
          wx.showToast({
            icon: "none",
            mask: true,
            title: "用户名错误或不存在",
          })
        }
      }
    
    })
  },
  zcChange1(event) {
    this.setData({
      zc1: event.detail.replace(/\s+/g, '')
    })
  },
  zcChange2(event) {
    this.setData({
      zc2: event.detail.replace(/\s+/g, '')
    })
  },
  // 注册
  rejs() {
    if (this.data.zc1 != ''&&this.data.zc2!='') {
       let {listdata}=this.data
       if(this.xiaoyan(this.data.zc1)){
        let flag=listdata.find(v=>v.username==this.data.zc1)
        if(flag){
        wx.showToast({
          title: '用户名已存在!',
          icon:'error'
        })
       }else{
        wx.request({
        url: 'http://localhost:7001/api/addLogin',
        method: 'post',
        data: {
          username: this.data.zc1,
          password: this.data.zc2,
          userid: this.data.listdata.length - 1
        },
        success: (res)=> {
            if(res.data.code==200){
              this.getLogins()
              this.setData({
                active:'b',
                zc1:"",
                zc2:""
              })
            }
        }
      })
       }
       }else{
        wx.showToast({
          title: '手机号为全数字!',
          icon:'error'
        })
       }
      
       
    }else{
      wx.showToast({
        title: '请填写完整!',
        icon:'error'
      })
    }
  },









  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})